package com.xinchao.controller;

import com.xinchao.constant.ResponseConstant;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.Advertiser;
import com.xinchao.model.User;
import com.xinchao.model.dto.AdvertiserListDTO;
import com.xinchao.model.page.PageInfo;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.service.AdvertiserService;
import com.xinchao.util.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 广告主Controller
 *
 * @author dxy
 * @date 2018/9/19 9:53
 */
@Controller
@RequestMapping(value = "/advertiser")
public class AdvertiserController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(AdvertiserController.class);
	@Autowired
	private AdvertiserService advertiserService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list")
	public String listAdvertiser(String advertiserName, Integer pageNum, Integer pageSize, Model model) {
		PageInfo<AdvertiserListDTO> pageInfo = advertiserService.listAdvertiser(advertiserName, pageNum, pageSize);
		model.addAttribute("advertiserName", advertiserName);
		model.addAttribute("advertiserList", pageInfo.getList());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("total", pageInfo.getTotal());
		return "advertiser_list";
	}

	/**
	 * 添加
	 */
	@ResponseBody
	@RequestMapping(value = "/save")
	public Object addAdvertiser(Advertiser advertiser, HttpServletRequest request) {
		if (StringUtils.isBlank(advertiser.getAdvertiserName())) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告主名称不能为空");
		}
		try {
			//验证广告主账号是否存在
			String userName = advertiser.getUserName();
			if (StringUtils.isNotBlank(userName)) {
				Advertiser oldAdvertiser = advertiserService.getAdvertiserByUserName(userName);
				if (oldAdvertiser != null) {
					return new JsonResponse(ResponseConstant.CODE_FAIL, "账号已存在，请重新输入账号");
				}
			}
			//登录用户
			User loginUser = getSessionUser(request);
			//远程登录ip
			String remortIP = HttpUtils.getRemortIP(request);
			//保存广告主
			advertiserService.saveAdvertiser(advertiser, loginUser, remortIP);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("AdvertiserController addAdvertiser 添加广告主失败：" + e.getMessage());
			return e;
		}
		return "添加广告主成功";
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping(value = "/update")
	public Object updateAdvertiser(Advertiser advertiser, HttpServletRequest request) {
		Long advertiserId = advertiser.getAdvertiserId();
		if (advertiserId == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告主ID不能为空，请联系管理员");
		}
		if (StringUtils.isBlank(advertiser.getAdvertiserName())) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告主名称不能为空");
		}
		try {
			//验证广告主账号是否存在
			String userName = advertiser.getUserName();
			if (StringUtils.isNotBlank(userName)) {
				Advertiser oldAdvertiser = advertiserService.getAdvertiserByUserName(userName);
				if (oldAdvertiser != null) {
					if (!advertiserId.equals(oldAdvertiser.getAdvertiserId())) {
						return new JsonResponse(ResponseConstant.CODE_FAIL, "账号已存在，请重新输入账号");
					}
				}
			}
			//登录用户
			User loginUser = getSessionUser(request);
			//远程登录ip
			String remortIP = HttpUtils.getRemortIP(request);
			//修改广告主
			advertiserService.updateAdvertiserById(advertiser, loginUser, remortIP);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("AdvertiserController updateAdvertiser 修改广告主失败：" + e.getMessage());
			return e;
		}
		return "修改广告主成功";
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public Object deleteAdvertiser(Long advertiserId, HttpServletRequest request) {
		if (advertiserId == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "广告主ID不能为空，请联系管理员");
		}
		//登录用户
		User loginUser = getSessionUser(request);
		//远程登录ip
		String remortIP = HttpUtils.getRemortIP(request);
		try {
			advertiserService.deleteAdvertiserById(advertiserId, loginUser, remortIP);
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("AdvertiserController deleteAdvertiser 删除广告主失败：" + e.getMessage());
			return e;
		}
		return "删除成功";
	}

	/**
	 * 获取广告主选项列表
	 */
	@ResponseBody
	@RequestMapping(value = "/listAdvertiserSelect")
	public Object listAdvertiserSelect() {
		return advertiserService.listAdvertiserSelect();
	}

}
